<?php

namespace app\admin\controller;

use think\Db;
use think\Request;

class Recharge extends Common
{

    // 储值卡列表
    public function index()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('recharge_card')
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('recharge_card')->listEndTimeInfo($list);
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    // 储值卡状态管理
    public function status()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('RechargeCard')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');

        }
    }
    // 储值卡删除
    public function del()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('RechargeCard')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }
    // 储值卡编辑
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('RechargeCard')->commonData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            $list = db('recharge_card')->where(['id' => $id])->find();
            $list['endtime'] = floor(($list['endtime']-$list['addtime'])/(24*3600));
            return $this->fetch('', ['list' => $list]);
        }
    }
    // 储值卡添加
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            return model('RechargeCard')->commonData($data, '添加成功', '添加失败，请重试');
        } else {
            return $this->fetch('');
        }
    }
    // 会员卡等级订单管理
    public function order()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $phone = input('post.phone');
            $status = input('post.status');
            $upgrede_number = input('post.upgrede_number');
            $where = array();
            if ($phone) {
                $where['u.phone'] = $phone;
            }
            if ($status) {
                $where['v.status'] = $status;
            }
            if ($upgrede_number) {
                $where['v.recharge_number'] = $upgrede_number;
            }
            $page = input('page') ? input('page') : 1;
            // dump($data);die();
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('recharge_card_order')
                ->alias('v')    
                ->join('user u', 'v.user_id = u.user_id')
                ->join('recharge_card c', 'v.card_id = c.id')
                ->where($where)
                ->field('u.realname as realname,v.recharge_number as recharge_number,v.id as id,u.phone as phone,u.vip_number as vip_number,c.title as title,v.money as money,v.status as status,v.addtime as addtime,c.number as number')
                ->order('v.id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach ($list['data'] as $i => $item) {
                $list['data'][$i]['addtime'] = date('Y-m-d', $item['addtime']);
            }
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'],'count'=>$list['total'], 'rel' => 1];
        } else {
            return $this->fetch('');
        }
    }
    public function orderDel()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('RechargeCardOrder')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }
}
